﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V4.Widget;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;
using Java.Interop;
using Medicine.Core.Listeners;
using static Android.Widget.AdapterView;

namespace Medicine.Activities
{
    [Activity(Label = "程序服务信息设置")]
    public class AppSetActivity : AppCompatActivity
    {
        private Models.AppSet _appSet;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here
            SetContentView(Resource.Layout.AppSetLayout);
            InitEvent();
            LoadAppSetOldData();
        }

        /// <summary>
        /// 绑定事件
        /// </summary>
        private void InitEvent()
        {
            Button button = FindViewById<Button>(Resource.Id.Btn_AppSetSubmit);
            button.Click += (s, e) => SaveData();
        }

        /// <summary>
        /// 加载旧数据
        /// </summary>
        private void LoadAppSetOldData()
        {
            var oldData = DbContext.DbC.FirstQuery<Models.AppSet>(f =>f.Id!=null&&f.Id!="");
            var allCount= DbContext.DbC.ListQuery<Models.AppSet>(f => f.Id != null && f.Id != "").Count;
            if (oldData == null)
            {
                _appSet = new Models.AppSet();
                _appSet.InitDefaultData();
            }
            else
            {
                _appSet = oldData;
            }
            BindData();
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            if (this._appSet != null)
            {
                var properties = _appSet.GetType().GetProperties();
                var resource = typeof(Resource.Id);
                var resourceFields = resource.GetFields();

                foreach (var item in properties)
                {
                    var txtName = $"Txt_{item.Name}";
                    var txtResource = resourceFields.FirstOrDefault(s => s.Name == txtName);
                    if (txtResource == null)
                        continue;

                    var tmpObj = resource.Assembly.CreateInstance(txtResource.ReflectedType.Name);
                    var txtId = txtResource.GetValue(tmpObj);
                    if (txtId != null)
                    {
                        int tmpTxtId = Convert.ToInt32(txtId);
                        var itemVal = item.GetValue(this._appSet);
                        if (itemVal == null)
                            continue;

                        //时间或日期格式特殊处理
                        //if (item.Name.Contains("Time") || item.Name.Contains("Date"))
                        //{
                        //    EditText dateTxt = FindViewById<EditText>(tmpTxtId);
                        //    if (dateTxt != null)
                        //        dateTxt.Text = Convert.ToDateTime(itemVal).ToString("yyyy-MM-dd");
                        //    continue;
                        //}

                        TextInputEditText txt = FindViewById<TextInputEditText>(tmpTxtId);
                        if (txt != null)
                            txt.Text = itemVal.ToString();
                    }
                }
            }
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        protected void SaveData()
        {
            InitData();
            int result = 0;
            if (!string.IsNullOrEmpty(this._appSet.Id))
            {
                result = DbContext.DbC.Update(this._appSet);
            }
            else
            {
                result = DbContext.DbC.InsertByBaseModel(this._appSet);
            }

            if (result > 0)
            {
                Helpers.MessageShow.ShowShortMsg(this, "保存成功");
                System.Threading.Tasks.Task.Delay(1000);
                this.Finish();
            }
            else
                Helpers.MessageShow.ShowShortMsg(this, "保存失败");
        }


        /// <summary>
        /// 自动建立将要保存的数据
        /// </summary>
        /// <returns></returns>
        protected void InitData()
        {
            if (_appSet == null)
            {
                _appSet = new Models.AppSet();
            }

            var properties = _appSet.GetType().GetProperties();
            var resource = typeof(Resource.Id);
            var t = resource.GetFields();
            foreach (var item in properties)
            {
                var txtName = $"Txt_{item.Name}";
                var txtResource = t.FirstOrDefault(s => s.Name == txtName);
                if (txtResource == null)
                    continue;
                var tmpObj = resource.Assembly.CreateInstance(txtResource.ReflectedType.Name);
                var txtId = txtResource.GetValue(tmpObj);
                if (txtId != null)
                {
                    int tmpTxtId = Convert.ToInt32(txtId);
                    ////时间或日期格式特殊处理
                    //if (item.Name.Contains("Time") || item.Name.Contains("Date"))
                    //{
                    //    EditText dateTxt = FindViewById<EditText>(tmpTxtId);
                    //    if (dateTxt != null)
                    //    {
                    //        var v = Convert.ToDateTime(dateTxt.Text);
                    //        item.SetValue(_appSet, v);
                    //    }
                    //    continue;
                    //}

                    TextInputEditText txt = FindViewById<TextInputEditText>(tmpTxtId);
                    if (txt != null)
                    {
                        item.SetValue(_appSet, txt.Text);
                    }
                }
                //TextInputEditText txt = FindViewById<TextInputEditText>();
            }
        }
    }

}